package cn.tuling.org.service.impl;
import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.Writer;
import cn.tuling.org.mapper.WriterMapper;
import cn.tuling.org.query.WriterQuery;
import cn.tuling.org.service.WriterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class WriterServiceImpl implements WriterService{
    @Autowired
    private WriterMapper writerMapper;

    @Override
    public List<Writer> findAll() {
        return null;
    }

    @Override
    @Transactional
    public void add(Writer writer) {
        writer.setCreattime(new Date());
        writerMapper.add(writer);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        writerMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(Writer writer) {
        writerMapper.update(writer);
    }

    @Override
    public PageList<Writer> queryPage(WriterQuery query) {
        // 查询全部
        Integer count = writerMapper.queryByCount(query);
        if (count == null) {
            return new PageList<Writer>(0, new ArrayList<>());
        }
        // 查询数据库
        List<Writer> data = writerMapper.queryByPage(query);
        return new PageList<>(count,data);
    }
}
